package cn.lg.soar.boot.nc.service.impl;

import cn.lg.soar.boot.nc.tool.FriendshipurlValidation;
import cn.lg.soar.common.data.msg.I18n;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.ParameterUtil;
import org.springframework.stereotype.Service;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.boot.nc.mapper.FriendshipurlMapper;
import cn.lg.soar.boot.nc.entity.Friendshipurl;
import cn.lg.soar.boot.nc.service.FriendshipurlService;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * 友情链接表
 * @author BoBo
 * @since 2025-02-19
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class FriendshipurlServiceImpl extends BaseServiceImpl<FriendshipurlMapper, Friendshipurl> implements FriendshipurlService {


    @Override
    public HttpResult<Serializable> addFriendshipurl(Friendshipurl friendshipurl) {
        ParameterUtil.notNull(friendshipurl, "数据不能为空");
        if (!FriendshipurlValidation.isValidateURL(friendshipurl.getUrladdress())) {
            return HttpResult.fail("B0000", I18n.build("url地址格式错误！"));
        }
        AssertUtil.isTrue(super.save(friendshipurl), "创建失败");
        return HttpResult.success("创建成功");
    }

    @Override
    public HttpResult<Serializable> updateFriendshipurl(Friendshipurl friendshipurl) {
        ParameterUtil.notNull(friendshipurl, "数据不能为空");
        if (!FriendshipurlValidation.isValidateURL(friendshipurl.getUrladdress())) {
            return HttpResult.fail("B0000", I18n.build("url地址格式错误！"));
        }

        AssertUtil.isTrue(super.updateById(friendshipurl), "更新失败");

        return HttpResult.success("更新成功");
    }

    @Override
    public HttpResult<Serializable> updateSort(List<Map<String, BigInteger>> sortedData) {
        if (null == sortedData || 0 >= sortedData.size()) {
            return HttpResult.fail("4000", "排序数据不可为空", "error");
        }

        sortedData.forEach(item-> {
            if (!item.containsKey("id") || !item.containsKey("orders")) {
                return;
            }
            Long id = item.get("id").longValue(); //类型转换
            int orders = item.get("orders").intValue(); //类型转换
            Friendshipurl friendshipurl = new Friendshipurl();
            friendshipurl.setId(id);
            friendshipurl.setOrders(orders);
            //执行修改
            super.updateById(friendshipurl);
        });
        return HttpResult.success("排序成功");
    }
}
